<template>
  <div class="login-box">
    <div class="login">
      <!-- logo -->
      <div class="logo">
        <img src="../assets/heima.png">
      </div>
      <!-- 表单区域 -->
      <el-form ref="resectRef" class="form" :model='form' :rules="rules">
        <el-form-item prop="username">
            <el-input prefix-icon="iconfont icon-user" v-model="form.username" @keyup.native.enter="emm"></el-input>
        </el-form-item>
        <el-form-item prop="password">
            <el-input prefix-icon="iconfont icon-3702mima" type="password" v-model="form.password" @keyup.native.enter="emm"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
            <el-button ref="btn" type="primary" @click="onlogin">登录</el-button>
            <el-button type="info" @click="resect">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 表单数据
      form: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则
      rules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  created () {

  },

  methods: {
    // 表单重置
    resect () {
      this.$refs.resectRef.resetFields()
    },
    // 表单登陆
    onlogin () {
      this.$refs.resectRef.validate(async flag => {
        // console.log(flag)
        if (!flag) return
        const { data: res } = await this.$axios.post('login', this.form)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        // console.log(res)
        this.$message.success(res.meta.msg)
        sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    },
    emm () {
      // console.log(this)
      this.onlogin()
    }
  }
}
</script>

<style scoped lang='less'>
.login-box {
  background: -webkit-linear-gradient(left, #00C9FF, #92FE9D);
  width: 100%;
  height: 100%;
}
.login {
  width: 450px;
  height: 300px;
  background: #fff;
  border-radius: 15px;
  position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.logo {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 3px 5px #008c8c;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 5px;
  img {
    width: 100%;
    height: 100%;
  }
}
.form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 30px;
  box-sizing: border-box;
  .btns {
    float: right;
    // padding-right: ;
  }
}
</style>
